package com.hrhr74.penguinstream.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hrhr74.penguinstream.domain.dto.AddressDTO;
import com.hrhr74.penguinstream.domain.po.Address;
import com.hrhr74.penguinstream.domain.vo.AddressVO;
import com.hrhr74.penguinstream.service.IAddressService;
import com.hrhr74.penguinstream.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {
    private final IAddressService addressService;

    /**
     * 新增地址
     * @param addressDTO
     * @return
     */
    @PostMapping("create")
    public Result<Void> addAddress(@RequestBody AddressDTO addressDTO) {
        return addressService.saveAddress(addressDTO);
    }

    /**
     * 查询地址
     * @param userId
     * @return
     */
    @PostMapping("list")
    public Result<List<AddressVO>> listAddress(@RequestParam Long userId){
        return addressService.listAddress(userId);
    }

    /**
     * 修改地址
     * @param addressDTO
     * @return
     */
    @PutMapping("update")
    public Result updateAddress(@RequestBody AddressDTO addressDTO){
        return addressService.updateAddress(addressDTO);
    }

    /**
     * 根据id查询地址
     * @param addressId
     * @return
     */
    @PostMapping("{id}")
    public Result<AddressVO> getAddress(@PathVariable("id") Long addressId){
        Address address = addressService.getById(addressId);
        return Result.ok(BeanUtil.copyProperties(address, AddressVO.class),"查询成功");
    }

    /**
     * 设置默认地址
     * @param addressId
     * @return
     */
    @PutMapping({"default"})
    public Result setDefault(@RequestParam Long addressId){
        return addressService.setDefault(addressId);
    }

    /**
     * 删除地址
     * @param addressId
     * @return
     */
    @DeleteMapping("delete")
    public Result deleteAddress(@RequestParam Long addressId){
        return addressService.deleteById(addressId);
    }


    /**
     * 获取默认
     * @return
     */
    @GetMapping("getDefault")
    public Result<AddressVO> getDefaultAddress(){
        return addressService.getDefaultAddress();
    }
}
